#!/bin/bash

cd `dirname $0`
MYNAME=`basename $0`

if [ "$MYNAME" == "build-test-config" ]; then
	echo "Creating config for test image"
	TEST=1
fi

if [ "$1" == "" ]; then
 	VERSION=`cat .piversion`
 	echo No version given, assuming Pi$VERSION
else
 	VERSION=$1
fi

echo $VERSION > .piversion

BRDIR=`./brdir $VERSION`

echo "Building in $BRDIR"
echo $VERSION > $BRDIR/.piversion
echo $VERSION > buildroot/PIVERSION

if [ "$VERSION" == "4" ]; then
	CFG="raspberrypi4_64_defconfig"
	UI=1
elif [ "$VERSION" == "3" ]; then
	CFG="raspberrypi3_64_defconfig"
	UI=1
elif [ "$VERSION" == "2" ]; then
        CFG="raspberrypi2_defconfig"
        UI=0
elif [ "$VERSION" == "02" ]; then
        CFG="raspberrypizero2w_defconfig"
        UI=0
elif [ "$VERSION" == "0" ]; then
        CFG="raspberrypi0_defconfig"
        UI=0
else
	echo "Pi version $1 not supported"
	exit 1
fi

pushd ../buildroot
make $CFG
popd

mv ../buildroot/.config $BRDIR/.config
cat configs/override.conf >> $BRDIR/.config
echo "BR2_PACKAGE_PIVERSION_$VERSION=y" >> $BRDIR/.config
# use full package set
echo "BR2_PACKAGE_HIFIBERRY_ALL=y" >> $BRDIR/.config

if [ "$UI" == "1" ]; then
	echo "BR2_PACKAGE_HIFIBERRY_LOCALBROWSER=y" >> $BRDIR/.config
fi

if [ "$TEST" == "1" ]; then
	echo "Changing to test config"
	cat configs/override-test.conf >> $BRDIR/.config
fi

# Update version
TS=`date +%Y%m%d`
echo $TS > buildroot/VERSION
